//
//  PublicDetailController.m
//  PropertyMananger
//
//  Created by bh on 17/1/20.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "PublicDetailController.h"

#import "RepairDetailCell.h"
#import "DescribeCell.h"
#import "UpLoadImgCell.h"
#import "ToRepairCell.h"
#import "TextViewCell.h"
#import "MyPublicOrderStatusCell.h"

@interface PublicDetailController ()<UITextViewDelegate>

@property (nonatomic,copy) NSString * textViewStr;//输入的处理结果

@end

@implementation PublicDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleViewName:@"公共报修单详情"];
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH - 64) isGroup:NO];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self initData];
    
}
- (void)initData{
    
    self.imageViews = [NSMutableArray array];
    
    for (PhotoMode * model in self.model.photoModeArray) {
        
        [self.imageViews addObject:model.PhotoName];
    }
//    self.urlsSmall = @[@"http://img1.imgtn.bdimg.com/it/u=321087086,3298231199&fm=206&gp=0.jpg",
//                       @"http://img3.imgtn.bdimg.com/it/u=1183223528,3058066243&fm=206&gp=0.jpg",
//                       @"http://img1.imgtn.bdimg.com/it/u=1825165654,1935296637&fm=206&gp=0.jpg"];
//    self.urlsBig = @[@"http://v1.qzone.cc/pic/201309/29/22/12/5248354649fec935.jpg%21600x600.jpg",
//                     @"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg",
//                     @"http://img2.3lian.com/img2007/19/33/005.jpg"];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if ([self.model.ProcessState isEqualToString:@"30"] || [self.model.ProcessState isEqualToString:@"10"]) {
        
        return 2;
    }else{
        
        return 1;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        if (![self.model.ProcessState isEqualToString:@"10"]) {
            
            return 10;
        }else{
            return 9;
        }
        
    }else{
        
        return 1;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        RepairDetailCell * cell = [RepairDetailCell cellWithTableView:tableView];
        
        if (indexPath.row == 0) {
            
            self.titleStr = @"订单编号:";
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.RepairCode];
            
            self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
            [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
            cell.infoLabel.attributedText = self.stra;
            
        }else if (indexPath.row == 1){
            
            self.titleStr = @"报修类型:";
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.CategoryName];
            
            self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
            [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
            cell.infoLabel.attributedText = self.stra;
            
        }else if (indexPath.row == 2){
            self.titleStr = @"报修人:";
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.LinkMan];
            
            self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
            [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
            cell.infoLabel.attributedText = self.stra;
            
        }else if (indexPath.row == 3){
            
            self.titleStr = @"报修人电话:";
            if (self.model.LinkMan == nil) {
                
               self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,@"暂无"];
            }else{
            
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.LinkTel];
                
                [cell.phoneBtn setImage:IMG(@"ico_dial_telephone") forState:UIControlStateNormal];
                [cell.phoneBtn addTarget:self action:@selector(phoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                cell.phoneBtn.tag = 300;
            }
            
            
            self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
            [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
            cell.infoLabel.attributedText = self.stra;
            
            
            
        }else if (indexPath.row == 4){
            
            self.titleStr = @"报修时间:";
            if (self.model.ApplyTime == nil) {
                self.pingjieStr = [NSString stringWithFormat:@"%@ %@",self.titleStr,@"暂无"];
                
            }else{
               
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.ApplyTime];
                
            }
            
            self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
            [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
            cell.infoLabel.attributedText = self.stra;
            
        }
        else if (indexPath.row == 5){
            
            DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
            
            cell.titleLabel.text = @"报修地址";
            cell.infoLabel.text = self.model.RepairAddr;
            
            return cell;
            
        }else if (indexPath.row == 6){
            
            DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
            
            cell.model = self.model;
            
            cell.titleLabel.text = @"问题描述";
            
            return cell;
            
        }else if (indexPath.row == 7){
            
            if (self.model.photoModeArray.count == 0) {
                
                DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
                
                cell.titleLabel.text = @"上传图片";
                cell.infoLabel.text = @"暂无图片";
                
                return cell;

            }else{
            
                UpLoadImgCell * cell = [UpLoadImgCell cellWithTableView:tableView];
                
                cell.urlsSmall = self.imageViews;
                cell.urlsBig = self.imageViews;
                
                //    DLog(@"图片数据 === %@",self.urlsBig);
                
                return cell;
            }
           
            
        }else if (indexPath.row == 8){//处理结果
            
            if ([self.model.ProcessState isEqualToString:@"30"]) {
                
                TextViewCell * cell = [TextViewCell cellWithTableView:tableView];
                self.textViewStr = cell.textView.text;
                
                return cell;
            }else if ([self.model.ProcessState isEqualToString:@"10"]){
                
                MyPublicOrderStatusCell * orderDetailCell = [MyPublicOrderStatusCell cellWithTableView:tableView];
                orderDetailCell.processState = self.model.ProcessState;
                return orderDetailCell;
                
            }else{
                //已处理状态
                DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
                
                cell.titleLabel.text = @"处理结果";
                cell.infoLabel.text = self.model.ReplyContent;
                
                return cell;
            }
        }
        else if (indexPath.row == 9){
            
            MyPublicOrderStatusCell * orderDetailCell = [MyPublicOrderStatusCell cellWithTableView:tableView];
            orderDetailCell.processState = self.model.ProcessState;
            return orderDetailCell;
            
        }
        
        return cell;
        
    }else if (indexPath.section == 1){//去维修
        
        ToRepairCell * cell = [ToRepairCell cellWithTableView:tableView];
        [cell.toRepairButton addTarget:self action:@selector(toRepairButton:) forControlEvents:UIControlEventTouchUpInside];
        
        if ([self.model.ProcessState isEqualToString:@"30"]) {
            
            [cell.toRepairButton setTitle:@"已处理" forState:UIControlStateNormal];
        }
        
        return cell;
        
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 5) {
   
        return [DescribeCell cellAddressHeightWiht:self.model];
            
        }else if (indexPath.row == 6){
        
            return [DescribeCell cellDescHeightWiht:self.model];
        
        }
        else if (indexPath.row == 7){
            if (self.model.photoModeArray.count == 0) {
                
               return 70;
                
            }else{
            return 130;
            }
            
            
        }else if (indexPath.row == 8){
            if ([self.model.ProcessState isEqualToString:@"30"]) {
                
                return 140;
                
            }else if ([self.model.ProcessState isEqualToString:@"100"]){
                
                return [DescribeCell cellMoreHeightWiht:self.model];

            }
            else{//待处理状态
                
               return [DescribeCell cellMoreHeightWiht:self.model];
            }
            
        }else if (indexPath.row == 9){
            
            return 90;
        }
        
        
    }else if (indexPath.section == 10){//去维修
        return 60;
    }
    return 50;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 0.001f;
    }else{
        return 20;
    }
    
}
- (void)phoneBtnClick:(UIButton *)button{
    
    DLog(@"点击了 === %ld",button.tag);
    if (button.tag == 300) {
        
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.LinkTel];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
        
    }
}
- (void)toRepairButton:(UIButton *)button{
    
    DLog(@"点击去维修");
    
    if ([self.model.ProcessState isEqualToString:@"30"]) {
        
        TextViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:8 inSection:0]];
        self.textViewStr = cell.textView.text;
        DLog(@" === %@",self.textViewStr);
        
    }
    
    [self postToRepairData];
}
#pragma mark - 去维修数据请求
- (void)postToRepairData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"正在提交数据...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    NSDictionary * parms = nil;
    if ([self.model.ProcessState isEqualToString:@"10"]) {
        
        parms =  @{
                   @"RepairID":self.model.RepairID,
                   @"ProcessState":@"30",
                   @"Content":@"",
                   @"json":@"\{\"jsonEntity\":\{},\"action\":\"handlerepair\",\"version\":\"V1.0\"}"
                   };
    }else if ([self.model.ProcessState isEqualToString:@"30"]){
        
        parms =  @{
                   @"RepairID":self.model.RepairID,
                   @"ProcessState":@"100",
                   @"Content":self.textViewStr,
                   @"json":@"\{\"jsonEntity\":\{},\"action\":\"handlerepair\",\"version\":\"V1.0\"}"
                   };
        
    }
    
    DLog(@"公共报修处理的结果提交的参数 === %@",parms);
    
    [MineRequest postGoToRepairSuccess:^(id data) {
        [MBProgressHUD hideHUDForView:self.view];
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        DLog(@"接单成功 == %@",dict);
        
        if ([[dict[@"Code"]stringValue] isEqualToString:@"1"]) {
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(didRefreshDataToRepair)]) {
                
                [self.delegate didRefreshDataToRepair];
            }
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        [MBProgressHUD showSuccess:dict[@"Msg"] time:1.5];
        
        [self.tableView reloadData];
    } Fail:^(NSString *errorDes) {
        
        [MBProgressHUD hideHUD];
        
    } param:parms ];
    
}
@end
